import { WeApp } from '../../../common/common';
import { config } from '../../../etc/config';
import httpClient from '../../../common/http-client';
import { OrderDetail, ShopCart, ISubmitOrderResult } from '../../../model/order';
import wxService from '../../../common/wxService'
import tools from '../../../common/tools'
import { MlcEnum } from '../../../model/enum'
import utils from '../../../utils/utils'
import { countDown } from '../../../assets/components/wux'
import orderHelper from '../../../utils/orderHelper';

const app = getApp();
interface OrderDetailPageData {
    order?: OrderDetail,
    btnDisabled?: boolean,
    btnLoading?: boolean,
    invoiceTypes?:Array<string>
}

interface OrderDetailPage extends IPage {

}

class OrderDetailPage {
    private orderId?: string;
    private c1;
    public data: OrderDetailPageData = {
        invoiceTypes: MlcEnum.toArray('InvoiceType')
    }
    public onLoad(option: {
        id?: string
    }): void {
        if (!option.id) {
            utils.showToastErr('参数错误');
            setTimeout(() => {
                wx.navigateBack();
            }, 900);
            return;
        }
        this.orderId = option.id
        this.loadData();
    }

    public loadData() {
        httpClient.get<OrderDetail>(tools.buildUrl(config.order.order_details, {
            'id': this.orderId
        }))
            .then((res) => {
                if (res.succeed) {
                    
                    if (res.value.status == MlcEnum.OrderStauts.待付款 ) {
                      this.c1 =  new countDown({
                            date: new Date(res.value.expire),
                            render(date) {
                                const hours = this.leadingZeros(date.hours, 2) + ' 时 '
                                const min = this.leadingZeros(date.min, 2) + ' 分 '
                                const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
                                this.setData({
                                    c1: hours + min + sec,
                                });
                            },
                            onEnd:()=>{
                                this.loadData();
                            }
                        })
                    }
                    this.setData({
                        'order': res.value,
                        'statusStr': MlcEnum.OrderStauts[res.value.status]
                    });
                } else {
                    wxService.showToast(res.msg);
                }
            });
    }

    public toAgain(e) {
        this.data.order.goodsInfo.forEach(item => {
            const params = {
                goodsId: item.goodsId,
                standardId: item.standardId
            };
            httpClient.post(config.order.shopcar_add, params);
        });
        wxService.switchTab('/pages/cart/list/index');
    }



    public toPay(e) {
        if (this.data.btnDisabled) {
            return;
        }
        utils.setBtnStatus(this);
        orderHelper.Pay(this.orderId,this.loadData).then(res => {
            utils.setBtnStatus(this);
        })

        // httpClient.post<ISubmitOrderResult>(config.order.order_pay_apply_wx, { ids: [this.orderId] })
        //     .then(res => {
        //         wx.hideLoading();
        //         utils.setBtnStatus(this);
        //         if (res.succeed) {
        //             utils.orderPayment(res.value, (pres) => {
        //                 utils.showToastCancel('支付未完成，请重新支付');
        //             },(pres)=>{
        //                 this.loadData();
        //             });
        //         } else {
        //             utils.showToastErr(res.msg);
        //         }
        //     });
    }

    public toCancel(e) {
        wx.showModal({
            title: '提示',
            content: '是否取消当前订单',
            cancelText: '放弃',
            success: (res) => {
                if (res.confirm) {
                    httpClient.post(config.order.order_cancle, { id: this.data.order.id })
                        .then(res => {
                            if (res.succeed) {
                                this.setData(
                                    {
                                        'order.status': MlcEnum.OrderStauts.已取消,
                                        'statusStr': '已取消'
                                    }
                                );
                            } else {
                                wxService.showToast(res.msg);
                            }
                        })
                }
            }
        })
    }

    afterServiceEventHandler(e) {
        const target = e.currentTarget.dataset;
        wxService.redirectTo(tools.buildUrl("/pages/afterService/apply/index", { orderId: target.orderId, standardIds: target.standardId }))
    }
}
Page(new OrderDetailPage());